/*
 *
 *  *  Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
 *  *  <p>
 *  *  Licensed under the GNU Lesser General Public License 3.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *  <p>
 *  * https://www.gnu.org/licenses/lgpl.html
 *  *  <p>
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

package com.dog.basics.admin.controller.codegen;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dog.basics.bean.model.codegen.GenConfig;
import com.dog.basics.common.util.HttpContextUtils;
import com.dog.basics.security.annotation.Inner;
import com.dog.basics.service.codegen.GenTableColumnService;
import com.dog.basics.service.codegen.GeneratorService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;

/**
 * 代码生成器
 *
 * @author guonh
 * @date 2018-07-30
 */
@RestController
@AllArgsConstructor
@RequestMapping("/gen/generator")
@Api(value = "代码生成模块", tags = "代码生成模块")
@ApiIgnore
public class GeneratorController {

    private final GeneratorService generatorService;

    private final GenTableColumnService columnService;

    /**
     * 列表
     *
     * @param tableName 参数集
     * @param dsName    数据源编号
     * @return 数据库表
     */
    @GetMapping("/page")
    public R getPage(Page page, String tableName, String dsName) {
        return R.ok(generatorService.getPage(page, tableName, dsName));
    }

    /**
     * 预览代码
     *
     * @param genConfig 数据表配置
     * @return
     */
    @GetMapping("/preview")
    public R previewCode(GenConfig genConfig) {
        return R.ok(generatorService.previewCode(genConfig));
    }

    /**
     * 查询表的列信息
     *
     * @param page      分页
     * @param genConfig 数据表配置
     * @return
     */
    @GetMapping("/table")
    public R getTable(Page page, GenConfig genConfig) {
        return R.ok(columnService.listTable(page, genConfig));
    }

    /**
     * 生成代码
     *
     * @param genConfig 数据表配置
     */
    @Inner
    @SneakyThrows
    @PostMapping("/code")
    public void generatorCode(@RequestBody GenConfig genConfig, HttpServletResponse response) {
        if(StrUtil.isBlank(genConfig.getStyle())){
            genConfig.setStyle("1");
        }
        byte[] data = generatorService.generatorCode(genConfig);
        // 设置编码
        HttpContextUtils.getHttpServletResponse().setCharacterEncoding("UTF-8");
        // 告诉前端输出流
        HttpContextUtils.getHttpServletResponse().setContentType("application/octet-stream; charset=UTF-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        HttpContextUtils.getHttpServletResponse().setHeader("Content-Disposition", String.format("attachment; filename=%s.zip", genConfig.getTableName()));
        // 设置文件大小
        HttpContextUtils.getHttpServletResponse().addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length));
        // 向外输出
        IoUtil.write(HttpContextUtils.getHttpServletResponse().getOutputStream(), Boolean.TRUE, data);
    }
}
